﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class EmployeeVacationAdd : FormBase
    {
        
        /// <summary>
        /// 修改前的信息
        /// </summary>
        private DBEntityEmoloyeeVacation employeeVacation_old = new DBEntityEmoloyeeVacation();
        /// <summary>
        /// 员工编码和名称之间的空格
        /// </summary>
        private readonly string codeNameEmpty = "  ";
        /// <summary>
        /// 当前窗体是插入模式，还是修改模式
        /// </summary>
        private bool frmInsert = true;
        /// <summary>
        /// 员工列表数据集
        /// </summary>
        private DataSet dsEmployee = new DataSet();

        public EmployeeVacationAdd()
        {
            InitializeComponent();

            frmInsert = true;
            initEmployeeList();
        }

        public EmployeeVacationAdd(DBEntityEmoloyeeVacation employeeVacation)
        {
            InitializeComponent();
            
            employeeVacation_old = (DBEntityEmoloyeeVacation)employeeVacation.Clone();
            frmInsert = false;
            initEmployeeList();

            int idx = comboxVacationState.Items.IndexOf(DBEntityEmoloyeeVacation.getVSTATEToStateStr(employeeVacation.VSTATE));
            if (idx != -1)
            {
                comboxVacationState.SelectedIndex = idx;
            }

            dateTimeStart.Value = employeeVacation.VSTARTDATETIME;
            if (employeeVacation.VSTARTDATETIME.Hour >= 7 && employeeVacation.VSTARTDATETIME.Hour <= 21)
            {
                comboStartTimeHour.SelectedIndex = employeeVacation.VSTARTDATETIME.Hour - 7;
            }
            comboStartTimeMinute.SelectedIndex = employeeVacation.VSTARTDATETIME.Minute;


            dateTimeEnd.Value = employeeVacation.VENDDATETIME;
            if (employeeVacation.VENDDATETIME.Hour >= 7 && employeeVacation.VENDDATETIME.Hour <= 21)
            {
                comboEndTimeHour.SelectedIndex = employeeVacation.VENDDATETIME.Hour - 7;
            }
            comboEndTimeMinute.SelectedIndex = employeeVacation.VENDDATETIME.Minute;

            DataRow[] drEmployee = dsEmployee.Tables[0].Select(DBEntityEmployee.FieldName.EMPLOYEEID + " = " + employeeVacation.EMPLOYEEID);
            if (drEmployee.Length != 0)
            {
                comboxEmployee.SelectedItem = drEmployee[0][DBEntityEmployee.FieldName.ECODE].ToString() + codeNameEmpty + drEmployee[0][DBEntityEmployee.FieldName.ENAME].ToString();
            }
        }

        public void initEmployeeList()
        {
            comboStartTimeHour.SelectedIndex = 0;
            comboEndTimeHour.SelectedIndex = 0;
            dateTimeStart.Value = DateTime.Now;
            dateTimeEnd.Value = DateTime.Now;
            comboxVacationState.SelectedIndex = 0;
            initComMinute(ref comboStartTimeMinute);
            initComMinute(ref comboEndTimeMinute);
            string error = "";
           
            if (!BussinessProxyManager.Instance.QueryEmployeeState( DBEntityEmployee.ENUM_ESTATE.F0E, out dsEmployee, out error))
            {
                MsgBox.MsgInfo(error);
                return;
            }

            comboxEmployee.Items.Clear();
            foreach (DataRow drEmployee in dsEmployee.Tables[0].Rows)
            {
                comboxEmployee.Items.Add(drEmployee[DBEntityEmployee.FieldName.ECODE].ToString() + codeNameEmpty + drEmployee[DBEntityEmployee.FieldName.ENAME].ToString());
            }
        }

        public void initComMinute(ref DevComponents.DotNetBar.Controls.ComboBoxEx cbxMinute)
        {
            for (int i = 0; i <= 59; i++)
            {
                cbxMinute.Items.Add(i.ToString());
            }
            cbxMinute.SelectedIndex = 0;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            bool bReturn = false;
            if (!checkInput(out error))
            {
                MsgBox.MsgInfo(error);
                return;
            }
            DataRow[] drEmployeeInfo = getEmployeeInfoForCodeName(comboxEmployee.SelectedItem.ToString(), dsEmployee);
            if (drEmployeeInfo.Length < 1 || drEmployeeInfo.Length == 0)
            {
                MsgBox.MsgInfo("无法找到正确的员工信息。");
                return;
            }
            string startDate = string.Concat(dateTimeStart.Value.ToShortDateString(), " ", comboStartTimeHour.SelectedItem.ToString(), ":", comboStartTimeMinute.SelectedItem.ToString(),":0");
            string endDate = string.Concat(dateTimeEnd.Value.ToShortDateString(), " ", comboEndTimeHour.SelectedItem.ToString(), ":", comboEndTimeMinute.SelectedItem.ToString(), ":0");
            DBEntityEmoloyeeVacation insertEmployeeVacation = new DBEntityEmoloyeeVacation();
            insertEmployeeVacation.EMPLOYEEID = Convert.ToInt32(drEmployeeInfo[0][DBEntityEmployee.FieldName.EMPLOYEEID]);
            insertEmployeeVacation.VSTARTDATETIME = Convert.ToDateTime(startDate);
            insertEmployeeVacation.VENDDATETIME = Convert.ToDateTime(endDate);
            insertEmployeeVacation.VSTATE = DBEntityEmoloyeeVacation.getStateStrToVSTATE(comboxVacationState.SelectedItem.ToString());

            if (frmInsert)
            {
                //插入状态               
                bReturn = BussinessProxyManager.Instance.InsertEmployeeVacation(insertEmployeeVacation, out error);
            }
            else
            {
                //修改状态
                bReturn = BussinessProxyManager.Instance.UpdateEmployeeVacation(employeeVacation_old, insertEmployeeVacation, out error);
            }
            if (!bReturn)
            {
                MsgBox.MsgInfo(error);
                return;
            }
            MsgBox.MsgInfo("保存成功。");
            this.DialogResult = DialogResult.OK;
        }

        private DataRow[] getEmployeeInfoForCodeName(string codeName, DataSet dsEmployeeInfo)
        {
            return dsEmployee.Tables[0].Select(string.Concat("(",DBEntityEmployee.FieldName.ECODE," + '", codeNameEmpty, "' + ", DBEntityEmployee.FieldName.ENAME,") = ", "'", codeName, "'"));
        }

        private bool checkInput(out string error)
        {
            error = string.Empty;
            if (comboxEmployee.SelectedItem == null || comboxEmployee.SelectedItem.ToString().Equals(string.Empty))
            {
                error = "请选择员工。";
                SetErrorControl(comboxEmployee, "请选择员工");
                return false;
            }
            if (dateTimeStart.Text.Equals(string.Empty))
            {
                error = "请输入开始时间。";
                SetErrorControl(dateTimeStart, "请输入开始时间");
                return false;
            }
            if (dateTimeEnd.Text.Equals(string.Empty))
            {
                error = "请输入结束时间。";
                SetErrorControl(dateTimeEnd, "请输入结束时间");
                return false;
            }
            if (comboxVacationState.SelectedItem.ToString().Equals(string.Empty))
            {
                error = "请选择休假状态。";
                SetErrorControl(comboxVacationState, "请选择休假状态");
                return false;
            }

            string startDate = string.Concat(dateTimeStart.Value.ToShortDateString(), " ", comboStartTimeHour.SelectedItem.ToString(), ":", comboStartTimeMinute.SelectedItem.ToString(), ":1");
            string endDate = string.Concat(dateTimeEnd.Value.ToShortDateString(), " ", comboEndTimeHour.SelectedItem.ToString(), ":", comboEndTimeMinute.SelectedItem.ToString(), ":1"); 
            
            if(DateTime.Now.CompareTo(Convert.ToDateTime(startDate)) > 0)
            {
                error = "休假的开始时间不能够小于当前时间。";
                SetErrorControl(dateTimeStart, "休假的开始时间不能够小于当前时间");
                return false;
            }
            if (DateTime.Now.CompareTo(Convert.ToDateTime(endDate)) >= 0)
            {
                error = "休假的结束时间不能够小于当前时间。";
                SetErrorControl(dateTimeEnd, "休假的结束时间不能够小于当前时间");
                return false;
            }
            if (Convert.ToDateTime(endDate).CompareTo(Convert.ToDateTime(startDate)) <= 0)
            {
                error = "休假的结束时间不能够小于或者等于休假的开始时间。";
                SetErrorControl(dateTimeEnd, "休假的结束时间不能够小于或者等于休假的开始时间");
                return false;
            }
            return true;
            
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (MsgBox.MsgQue("是否要退出？"))
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }

      
    }
}
